#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Security.Permissions;
using System.Security.Principal;
using System.Threading;
using AirWeb.Security;
using AirWeb.Security.FileSecurity;
using AirWeb.ApplicationTools.Common;
using AirWeb.Support;
using AirWeb.Support.Diagnostics;
using AirWeb.DirectoryMgr;

namespace FlashBack.Web 
{
	/// <summary>
	/// Summary description for Global.
	/// </summary>
	public class Global : System.Web.HttpApplication
	{

		string ConfigDir
		{
			get
			{
				return Context.Request.PhysicalApplicationPath + "config";
			}
		}

		protected void Application_Start(Object sender, EventArgs e)
		{

			// Initialize the application configuration
			ApplicationConfiguration.OnApplicationStart(Context.Server.MapPath( Context.Request.ApplicationPath ));

			ApplicationLog.WriteTrace("Application Start");

			// Make sure that ApplicationLog class is instantiated correctly.
			//if( ApplicationLog.InitException != null )
			//	throw ( ApplicationLog.InitException );

			// Try to load the account repository from the config file
			AirWeb.Security.SecurityMgr.Instance.SerializationPath = ConfigDir + "\\Credentials.xml";
			try
			{
				AirWeb.Security.SecurityMgr.Instance.Deserialize();
				ApplicationLog.WriteTrace("User account repository loaded from: " + AirWeb.Security.SecurityMgr.Instance.SerializationPath);
			}
			catch( Exception ex)
			{
				ApplicationLog.WriteError("Unable to load the user account repository. Reason: " + ex.Message);
				ApplicationLog.WriteTrace("Attempting to create new user account repository...");

				// Create a new administrator account 
				AirWeb.Security.Role r = AirWeb.Security.SecurityMgr.Instance.AddRole("Administrator", "System Administrator");
				FilePathResource fpr = new FilePathResource("~/");
				r.AddAllowRight(new AirWeb.Security.Right(fpr, AirWeb.Security.Right.AccessLevel.Read));
				AirWeb.Security.SecurityMgr.Instance.AddUser("admin", "password", "", "Administrator", new ExtraUserData(true));

				try
				{
					AirWeb.Security.SecurityMgr.Instance.Serialize();
				}
				catch(System.UnauthorizedAccessException ua)
				{
					//throw ua;  // Let the error page handle this exception
				}
				catch(Exception newEx)
				{
					ApplicationLog.WriteError("Unable to create new user account repository. Reason: " + newEx.Message);
					return;
				}

				ApplicationLog.WriteTrace("User account repository created. Created Administrator account - un:admin pw:password.");
			}
		}
 
		protected void Session_Start(Object sender, EventArgs e)
		{
			// If "Remember Me" is on we have already user without going through Login
			if(Context.User.Identity.Name != "")
			{
				AirWeb.Security.User u = AirWeb.Security.SecurityMgr.Instance.GetUserByName(Context.User.Identity.Name);
				if(u != null)
				{
					Session["KEY_CACHE_CURRENT_USER"] = u; // cache the logged in user to the session
				}
				else
				{
					Server.Transfer("LoginPage.aspx");
				}
			}
		}

		protected void Application_BeginRequest(Object sender, EventArgs e)
		{

		}

		protected void Application_EndRequest(Object sender, EventArgs e)
		{

		}

		protected void Session_End(Object sender, EventArgs e)
		{
			ApplicationLog.WriteTrace("Application End");
		}

		protected void Application_End(Object sender, EventArgs e)
		{
			
		}
		
		protected void Application_Error(Object sender, EventArgs e)
		{
			Application["LastError"] = Server.GetLastError();
			Server.Transfer("ApplicationError.aspx");
		}
	}
}

